/**
 * 
 */
package com.toncen.samepms.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

import com.sky.common.date.DateTools;
import com.sky.common.quartz.QuartzManager;

/**
 * 描述：Job抽象类
 * 
 * @author tch
 * 
 */
public abstract class AbstractJob implements StatefulJob {
	/**
	 * 执行次数记录器
	 */
	private volatile static Map<String, Long> fireCountMap = new HashMap<String, Long>(5);

	/**
	 * 上次执行时间记录器
	 */
	private volatile static Map<String, Date> lastFireTimeMap = new HashMap<String, Date>(5);

	/**
	 * 下次次执行时间记录器
	 */
	private volatile static Map<String, Date> nextFireTimeMap = new HashMap<String, Date>(5);

	/**
	 */
	protected Log logger = LogFactory.getLog(this.getClass());

	/**
	 * 
	 */
	public AbstractJob() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
	 */
	public synchronized void execute(JobExecutionContext context) throws JobExecutionException {
		if (context != null) {
			log(context);
			logger.debug("开始启动【" + context.getJobDetail().getName() + "】任务线程...");
		}
		long start = System.currentTimeMillis();
		run(context);
		if (context != null) {
			logger.debug("处理【" + context.getJobDetail().getName() + "】任务耗时:" + ((System.currentTimeMillis() - start) / 1000) + "s");
			logger.debug("成功结束【" + context.getJobDetail().getName() + "】任务线程...");
		}
	}

	/**
	 * 立即执行
	 * 
	 * @param jobName
	 * @throws Exception
	 */
	public static void triggerJob(String jobName) throws Exception {
		QuartzManager.triggerJob(jobName);// 立即执行一次
	}

	/**
	 * 获取执行次数
	 * 
	 * @param jobClass
	 * @param jobName
	 * @return
	 */
	public static Long getFireCount(Class jobClass, String jobName) {
		Long count = fireCountMap.get(getKey(jobClass, jobName));
		if (count == null) {
			count = 0L;
		}
		return count;
	}

	/**
	 * 获取上次执行日期
	 * 
	 * @param jobClass
	 * @param jobName
	 * @return
	 */
	public static Date getLastFireTime(Class jobClass, String jobName) {
		return lastFireTimeMap.get(getKey(jobClass, jobName));
	}

	/**
	 * 获取下次执行日期
	 * 
	 * @param jobClass
	 * @param jobName
	 * @return
	 */
	public static Date getNextFireTime(Class jobClass, String jobName) {
		return nextFireTimeMap.get(getKey(jobClass, jobName));
	}

	/**
	 * 执行方法
	 * 
	 * @param context
	 * @throws Exception
	 */
	protected abstract void run(JobExecutionContext context);

	/**
	 * 获取key
	 * 
	 * @param jobName
	 * @return
	 */
	protected static String getKey(Class jobClass, String jobName) {
		StringBuffer key = new StringBuffer();
		key.append(jobClass.getClass().getName());
		key.append("#");
		key.append(jobName);
		return key.toString();
	}

	/**
	 * 记录执行日志
	 * 
	 * @param context
	 */
	protected void log(JobExecutionContext context) {
		String key = getKey(context.getJobDetail().getJobClass(), context.getJobDetail().getName());
		long count = 1;
		if (fireCountMap.get(key) != null) {// 没有登记
			count = fireCountMap.get(key) + 1;
		}
		fireCountMap.put(key, count);
		lastFireTimeMap.put(key, context.getFireTime());
		nextFireTimeMap.put(key, context.getNextFireTime());
		System.out.println("开始第【" + count + "】次任务【" + context.getJobDetail().getName() + "】时间【" + DateTools.getDateTime(context.getFireTime()) + "】预计下次启动【"
				+ DateTools.getDateTime(context.getNextFireTime()) + "】...");
	}
}
